<template>
    <div class="e-form" v-if="state === 'main'">
        <!-- <div class="title">
                <img src="@/assets/test.png" alt="" />
                <span>外包商入库</span>
                <i class="el-icon-full-screen" @click="handleFullScreen"></i>
            </div> -->
        <div class="tabs">
            <div class="tabs-title">基本信息</div>
            <div style="width: 100%" class="form">
                <el-form :model="formData" label-width="170px" :rules="rules" ref="ruleForm">
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="招标编号：">
                                <div style="display: flex">
                                    <el-input v-model="formData.tenderApplyNo" disabled></el-input>
                                    <ComShowBiddingButton :tenderId="tenderId" mod="equipment" />
                                </div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="招标单位">
                                <span>{{ formData.tenderOrgName }}</span>
                            </el-form-item>
                        </el-col>
                        <!-- <el-col :span="12">
                            <el-form-item label="拟招标项目名称：">
                                <span>{{ formData.baseInfo.projectName }}</span>
                            </el-form-item>
                        </el-col> -->
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="拟招标项目名称：">
                                <span>{{ formData.projectName }}</span>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="录入日期：">
                                <span>{{ gmtCreate }}</span>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="采购方式：">
                                <span v-if="formData.tenderForm">{{
                                    formData.tenderForm | tenderForm(this)
                                }}</span>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="招标类型：">
                                <span>{{ tenderClass | tenderClass(this) }}</span>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="包件名称：" prop="packageName">
                                <el-select
                                    v-model="formData.packageName"
                                    filterable
                                    placeholder="请选择"
                                    @change="selectChange($event, '包件名称')"
                                >
                                    <el-option
                                        v-for="item in packageNameOptions"
                                        :key="item.dtlId"
                                        :label="item.name"
                                        :value="item.name"
                                    >
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="通知对象：" prop="noticeUser">
                                <el-select
                                    v-model="formData.noticeUser"
                                    filterable
                                    placeholder="请选择"
                                    @change="selectChange($event, '通知对象')"
                                >
                                    <el-option
                                        v-for="item in noticeUserNameOptions"
                                        :key="item.candidateId"
                                        :label="item.candidateName"
                                        :value="item.candidateName"
                                    >
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="是否中标：" prop="isBid">
                                <el-radio v-model="formData.isBid" :label="0">未中标</el-radio>
                                <el-radio v-model="formData.isBid" :label="1">已中标</el-radio>
                                <!-- <span>{{ formData.baseInfo.isBid === 0 ? '否' : '是' }}</span> -->
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="状态：">
                                <span>草稿</span>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="备注：">
                                <el-input
                                    type="textarea"
                                    :rows="2"
                                    placeholder="请输入内容"
                                    v-model="formData.remarks"
                                    style="margin-right: 110px"
                                >
                                </el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
        </div>
        <div class="buttons">
            <!-- <span class="tip"
                    ><span style="color: red; margin-right: 0">*</span
                    >为必填项</span
                > -->
            <!-- <el-button
                    type="primary"

                    class="btn-blue"
                    >打印</el-button
                >
                <el-button
                    type="primary"

                    class="btn-blue"
                    >删除</el-button
                >
                <el-button
                    type="primary"

                    class="btn-blue"
                    >发布</el-button
                >
                <el-button
                    type="primary"

                    class="btn-blue"
                    >审核</el-button
                >
                <el-button
                    type="primary"

                    class="btn-blue"
                    >撤回</el-button
                > -->
            <el-button type="primary" class="btn-blue" @click="submit">保存</el-button>
            <el-button @click="handleClose">取消</el-button>
        </div>
    </div>
</template>

<script>
// import ComTreeTable from '@/templates/table/treeTable.vue'
// import ComTreeFile from '@/templates/table/treeFile.vue'
// import ComPagination from '@/components/pagination/pagination.vue'
// import bidNoticeRequest from '@/api/tender/bidNotice.js'
import applyRequest from '@/api/tender/apply.js'
import equipmentProcurementRequest from '@/api/tender/equipmentProcurement.js'
import ComShowBiddingButton from '@/pages/bidding/common/showBiddingButton.vue'
import { mapState } from 'vuex'

export default {
    data () {
        return {
            state: 'main',
            // dialogVisible: false,
            // fullscreen: false,
            form: {
                name: '',
                region: '',
                date1: '',
                date2: '',
                delivery: false,
                type: [],
                resource: '',
                desc: ''
            },
            tableData: [],
            value: '',
            textarea: '',
            formData: {
                isBid: 1,
                noticeUser: '',
                noticeUserId: '',
                packageId: '',
                packageName: '',
                projectName: '',
                remarks: '',
                tenderApplyId: '',
                tenderApplyNo: '',
                tenderForm: '',
                tenderOrgId: '',
                tenderOrgName: ''
            },
            test: '',
            selectData: [],
            selectTableColumnAttr: [
                {
                    label: '单据编号',
                    prop: 'billNo'
                },
                {
                    label: '拟招标项目名称',
                    prop: 'tenderName'
                },
                {
                    label: '单据日期',
                    prop: 'gmtCreate'
                },
                {
                    label: '单据机构名称',
                    prop: 'orgName'
                }
            ],
            selectTableColumnWidth: {
                leftWidth: '150',
                rightWidth: '100'
            },
            selectDataPagination: {},
            gmtCreate: '',
            tenderClass: 3,
            noticeUserNameOptions: [],
            packageNameOptions: [],
            rules: {
                isBid: {
                    required: true,
                    message: '请选择是否中标',
                    trigger: 'change'
                },
                noticeUser: {
                    required: true,
                    message: '请选择通知对象',
                    trigger: 'change'
                },
                packageName: {
                    required: true,
                    message: '请选择包件名称',
                    trigger: 'change'
                }
            },
            front: {}
        }
    },
    computed: {
        ...mapState({
            options: state => state.biddingApplication.dictionaries
        }),
        tenderId () {
            return this.$route.query.tenderId || this.$route.query.billid
        }
    },
    components: {
        // ComTreeTable,
        // ComTreeFile,
        // ComPagination,
        ComShowBiddingButton
    },
    created () {
        this.init()
        // this.getCandidate() // 获取通知对象下拉列表
        this.getPackage() // 获取包件下拉列表
    },
    mounted () {},
    methods: {
        getCandidate () {
            equipmentProcurementRequest
                .noticeGetCandidate({ packageId: this.formData.packageId })
                .then(res => {
                    this.noticeUserNameOptions = res
                })
        },
        getPackage () {
            equipmentProcurementRequest
                .noticeGetPackagey({ tenderApplyId: this.tenderId })
                .then(res => {
                    this.packageNameOptions = res
                })
        },
        dateChange (value) {
            if (value === 'startTime') {
                this.formData.baseInfo.startTime = this.formData.baseInfo.startTime
                    .toLocaleDateString()
                    .replace(new RegExp('/', 'g'), '-')
            } else {
                this.formData.baseInfo.endTime = this.formData.baseInfo.endTime
                    .toLocaleDateString()
                    .replace(new RegExp('/', 'g'), '-')
            }
        },
        getNowDate () {
            // 个位数前补0
            const zeroFill = function (num) {
                if (parseInt(num) < 10) {
                    num = '0' + num
                }
                return num
            }
            let nowtime = new Date()
            nowtime =
                zeroFill(nowtime.getFullYear()) +
                '-' +
                zeroFill(nowtime.getMonth() + 1) +
                '-' +
                zeroFill(nowtime.getDate())
            // 前端展示，不用传给后端
            this.gmtCreate = nowtime
        },
        selectChange (name, label) {
            if (label === '包件名称') {
                this.packageNameOptions.forEach(item => {
                    if (item.name === name) {
                        this.formData.packageId = item.dtlId
                    }
                })
                this.getCandidate()
            } else if (label === '通知对象') {
                this.noticeUserNameOptions.forEach(item => {
                    if (item.candidateName === name) {
                        this.formData.noticeUserId = item.candidateId
                    }
                })
            }
        },
        async init () {
            await equipmentProcurementRequest.applyGetById({ id: this.tenderId }).then(res => {
                // 招标申请id
                this.formData.tenderApplyId = res.billId
                // 招标申请编号
                this.formData.tenderApplyNo = res.billNo
                // 	拟招标项目名称
                this.formData.projectName = res.tenderName
                // 	采购方式
                this.formData.tenderForm = res.tenderForm
                // 招标类型
                this.formData.tenderClass = res.tenderClass
                //  录入日期
                this.formData.reportTime = this.getNowDate()
                // 	招标机构
                this.formData.tenderOrgId = res.tenderUserId
                this.formData.tenderOrgName = res.tenderUser
                // 申请机构
                // this.org.applyOrgName = res.applyOrgName
                // 录入机构
                // this.front.orgName = res.orgName
            })
        },
        showBidding () {
            if (!this.tenderId) return // 前期需求可切换，现在不需要
            this.$notify.closeAll()
            this.$notify({
                title: '查看招标详情',
                dangerouslyUseHTMLString: true,
                message: `正式环境：客户端打开新页面<br>
                测试环境：手动打开新页面<br>
                <textarea cols="35" rows="10">http://localhost:8080/biddingApplicationShow?tenderId=${this.tenderId}</textarea>
                `,
                duration: 0
            })
        },
        selectCurrentChange (page) {
            this.tenderApplyQueryPage({ limit: 20, page })
        },
        selectDataSearch (searchValue) {
            this.tenderApplyQueryPage({ key: searchValue, limit: 20, page: 1 })
        },
        selectDataConfirm (data) {
            this.state = 'main'
            this.formData.baseInfo.applyOrgName = data.orgName
            this.formData.baseInfo.applyOrgId = data.orgId
        },
        tenderApplyNoInputFocus () {
            this.$refs.tenderApplyNoInput.blur()
        },
        stateChange (name) {
            this.state = name
            if (name === 'selectData') {
                this.tenderApplyQueryPage()
            }
        },
        tenderApplyQueryPage (params = { limit: 20, page: 1 }) {
            applyRequest.tenderApplyQueryPage(params).then(res => {
                this.selectData = res.list
                this.selectDataPagination.total = res.totalCount
            })
        },
        // dialogCloseHook () {
        //     this.initData()
        // },
        // initData () {
        //     this.formData = {
        //         state: 0
        //     }
        // },
        submit () {
            this.$refs.ruleForm.validate(valid => {
                if (valid) {
                    equipmentProcurementRequest.noticeCreate(this.formData).then(res => {
                        const callBack = () => {
                            this.$router.push({
                                path: './equipmentBidWinningNoticeEdit',
                                query: { billid: res }
                            })
                        }
                        const cancelCallBack = () => {
                            this.$router.push({
                                path: './equipmentBiddingApplicationEdit',
                                query: { billid: this.tenderId, seat: 'bidWinningNotice' }
                            })
                        }
                        this.clientPop(
                            'suc',
                            '保存成功，是否继续？',
                            callBack,
                            null,
                            cancelCallBack
                        )
                    })
                } else {
                    return false
                }
            })
        },
        // getVuexData () {
        //     // 获取保存在vuex的数据
        //     this.formData = {
        //         ...this.formData,
        //         ...JSON.parse(
        //             JSON.stringify(
        //                 this.$store.state.biddingApplication.formData
        //             )
        //         )
        //     }
        //     // 获取保存在vuex的常量
        //     if (Object.keys(this.$store.state.userInfo.orgInfo).length !== 0) {
        //         this.formData.baseInfo.orgId = this.$store.state.userInfo.orgInfo.orgId
        //         this.formData.baseInfo.orgName =
        //             this.$store.state.userInfo.orgInfo.orgName
        //     }
        //     console.log('this.formData1', this.formData)
        // },
        handleFullScreen () {
            this.fullscreen = !this.fullscreen
        },
        handleClose () {
            this.$router.push({
                path: './equipmentBiddingApplicationEdit',
                query: { billid: this.tenderId, seat: 'bidWinningNotice' }
            })
        },
        cellClsNm ({ column }) {
            if (column.label === '') {
                return 'fixed-left'
            }
        },
        cellMouseEnter (row) {
            this.MouseEnterId = row.id
        },
        cellMouseLeave () {
            this.MouseEnterId = 0
        }
    }
}
</script>

<style lang="scss" scoped>
.buttons {
    left: 0;
}
.e-form {
    padding: 0 20px;
    /* .tabs-title::before {
        content: '';
        height: 22px;
        width: 8px;
        border-radius: 40px;
        background-color: #2e61d7;
        display: block;
        position: absolute;
        left: 20px;
        margin-right: 20px;
    } */
}
.form {
    margin-top: 10px;
}

.e-table {
    background: #fff;

    /* .top {
    .left {
      padding: 20px;
      padding-top: 0px;
    }
  } */
}

.upload {
    margin: 20px auto;
    display: flex;
    justify-content: center;
    text-align: center;
}

.upload-demo {
    display: flex;
    justify-content: center;
    align-items: center;
}
</style>
